/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cloudclient.helper;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author TimotiusNC
 */
public class Utilities {
	private Utilities(){

	}

	public static Utilities getInstance(){
		if(m_instance == null)
			m_instance = new Utilities();

		return m_instance;
	}

	public long getCurrentTime(){
		return Calendar.getInstance().getTimeInMillis();
	}

	public void goToPanel(JFrame frame, JPanel panelToRemove, JPanel panelToInsert){
		frame.add(panelToInsert);
		frame.remove(panelToRemove);

		//Repaint, refresh, revalidate, whatever it is
		frame.invalidate();
		frame.repaint();
		panelToInsert.revalidate();
	}

	public void getListOfFile(ArrayList<File> listFiles, File dirPath){
		File[] childrenFiles = dirPath.listFiles();
		for(int i=0; i<childrenFiles.length; ++i){
			if(childrenFiles[i].isDirectory())
				getListOfFile(listFiles, childrenFiles[i]);
			else
				listFiles.add(childrenFiles[i]);
		}
	}

	public void debugHashMap(ConcurrentHashMap hmap){
		System.out.println("\thashmap debugging");
		Set keys = hmap.keySet();
		Iterator keysIt = keys.iterator();

		while(keysIt.hasNext()){
			File key = (File) keysIt.next();
			System.out.println("\t" + key + "\t" + hmap.get(key));
		}
	}

	public void populateHashMapTextArea(HashMap hmap, JTextArea txtArea){
		StringBuilder sb = new StringBuilder();
		Set keys = hmap.keySet();
		Iterator keysIt = keys.iterator();

		while(keysIt.hasNext()){
			File file	= (File) keysIt.next();
			String time	= hmap.get(file).toString();

			sb.append(file.getName()).append("\t").append(time).append("\r\n");
		}

		txtArea.setText(sb.toString());
	}

	public void populateTableModel(HashMap hmap, DefaultTableModel tabModel){
		Set keys = hmap.keySet();
		Iterator keysIt = keys.iterator();

		while(keysIt.hasNext()){
			File file	= (File) keysIt.next();
			String time	= hmap.get(file).toString();

			tabModel.addRow(new Object[]{file.getName(), time});
		}
	}

	//DATA
	public static final String m_fileHashName = ".cfi";

	//INSTANCE
	private static Utilities m_instance;
}
